package com.ruoyi.dataMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dataMain.domain.bo.DataAccountBo;
import com.ruoyi.dataMain.domain.vo.DataAccountVo;
import com.ruoyi.dataMain.domain.DataAccount;
import com.ruoyi.dataMain.mapper.DataAccountMapper;
import com.ruoyi.dataMain.service.IDataAccountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 开户总表Service业务层处理
 *
 * @author linziheng
 * @date 2023-10-28
 */
@RequiredArgsConstructor
@Service
public class DataAccountServiceImpl implements IDataAccountService {

    private final DataAccountMapper baseMapper;

    /**
     * 查询开户总表
     */
    @Override
    public DataAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }
    /**
     * 查询开户总表by 帐户id
     */
    @Override
    public DataAccountVo queryByAccountId(String accountId){
        DataAccountBo bo = new DataAccountBo();
        bo.setAccountId(accountId);
        return baseMapper.selectVoOne(buildQueryWrapper(bo));
    }
    /**
     * 查询开户总表列表
     */
    @Override
    public TableDataInfo<DataAccountVo> queryPageList(DataAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataAccount> lqw = buildQueryWrapper(bo);
        Page<DataAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询开户总表列表
     */
    @Override
    public List<DataAccountVo> queryList(DataAccountBo bo) {
        LambdaQueryWrapper<DataAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataAccount> buildQueryWrapper(DataAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataAccount> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMedium()), DataAccount::getMedium, bo.getMedium());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), DataAccount::getAccountId, bo.getAccountId());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), DataAccount::getAccountName, bo.getAccountName());
        lqw.like(StringUtils.isNotBlank(bo.getMainName()), DataAccount::getMainName, bo.getMainName());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplier()), DataAccount::getSupplier, bo.getSupplier());
        lqw.eq(bo.getCustomId() != null, DataAccount::getCustomId, bo.getCustomId());
        return lqw;
    }

    /**
     * 新增开户总表
     */
    @Override
    public Boolean insertByBo(DataAccountBo bo) {
        DataAccount add = BeanUtil.toBean(bo, DataAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改开户总表
     */
    @Override
    public Boolean updateByBo(DataAccountBo bo) {
        DataAccount update = BeanUtil.toBean(bo, DataAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除开户总表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
